﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Model;
using EntityServices;
using System.Web.Http.Cors;

namespace WebApp1.Controllers
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class 班级Controller : ApiController
    {
        TimeTableOneEntityServicesFactory entityServices = new TimeTableOneEntityServicesFactory();
        // GET api/<controller>

        [HttpGet]
        [Route("api/班级")]
        public IEnumerable<班级> Get()
        {
            return entityServices.班级Service.GetList();
        }

        // GET api/<controller>/5
        public 班级 Get(int id)
        {
            return entityServices.班级Service.GetById(id);
        }

        [HttpPost]
        [Route("api/班级/{cId}")]
        // POST api/<controller>
        public void Post([FromBody]班级 value,int cId)
        {
            系部 temp = entityServices.系部Service.GetById(cId);
            if (temp != null)
                value.系部 = temp;
            entityServices.班级Service.NewSave(value);
        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]班级 value)
        {
            if (this.Get(id) != null)
            {
                Delete(id);
                entityServices.班级Service.NewSave(value);

            }
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
            班级 temp = this.Get(id);
            if (temp != null)
                entityServices.班级Service.Remove(temp);
        }

        //取回这个班级的课表
        [HttpGet]
        [Route("api/班级/{cId}/课表")]
        public ICollection<课程>  GetCourseTable(int cId)
        {
           // ICollection<课程课表> temp=entityServices.班级Service.GetById(cId).课程课表;
            return entityServices.班级Service.GetById(cId).课程;
        }

        [HttpGet]
        [Route("api/班级/{cId}/教师")]
        public ICollection<教师> GetTeacher(int cId)
        {
            return entityServices.班级Service.GetById(cId).系部.教师;
        }

        [HttpPost]
        [Route("api/班级/添加班级课程")]
        public void AddCourse([FromBody]课程 value)
        {
            value.班级 = entityServices.班级Service.GetById(Convert.ToInt32(value.班级Id));
            value.教师=entityServices.教师Service.GetById(Convert.ToInt32(value.教师Id));
            entityServices.课程Service.NewSave(value);
            //entityServices.班级Service.GetById(Convert.ToInt32(value.班级Id)).课程.Add(value);
        }
    }
}